package com.sample.library.glide;

import android.content.Context;
import android.widget.ImageView;

import androidx.databinding.BindingAdapter;

import com.azhon.appupdate.utils.DensityUtil;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.ruffian.library.widget.RTextView;
import com.sample.library.R;
import com.sample.library.ext.ExtsKt;


/**
 * @author jingbin
 * @date 2019/04/29
 */

public class GlideUtil {


    /**
     * 显示随机的图片(每日推荐)
     *
     * @param imgNumber 有几张图片要显示,对应默认图
     * @param imageUrl  显示图片的url
     * @param imageView 对应图片控件
     */
    public static void displayRandom(int imgNumber, String imageUrl, ImageView imageView) {
        Glide.with(imageView.getContext())
                .load(imageUrl)
                .placeholder(R.mipmap.ic_launcher_round)
                .error(R.mipmap.ic_launcher_round)
                .transition(DrawableTransitionOptions.withCrossFade(1500))
                .into(imageView);
    }


    /**
     * 显示高斯模糊效果（电影详情页）
     */
    private static void displayGaussian(Context context, String url, ImageView imageView) {
        // "23":模糊度；"4":图片缩放4倍后再进行模糊
        Glide.with(context)
                .load(url)
                .transition(DrawableTransitionOptions.withCrossFade())
                .error(R.mipmap.ic_launcher_round)
                .placeholder(R.mipmap.ic_launcher_round)
                .transition(DrawableTransitionOptions.withCrossFade(500))
                //.transform(new BlurTransformation(50, 8))
                .into(imageView);
    }

    /**
     * 加载圆角图,暂时用到显示头像
     */
    @BindingAdapter("android:displayCircle")
    public static void displayCircle(ImageView imageView, String imageUrl) {
        Glide.with(imageView.getContext())
                .load(imageUrl)
                .transition(DrawableTransitionOptions.withCrossFade(500))
                .error(R.mipmap.ic_launcher_round)
                .transform(new CircleCrop())
//                .apply(bitmapTransform(new CircleCrop()))
//                .transform(new GlideCircleTransform())
//                .transform(new RoundedCorners(20))
//                .transform(new CenterCrop(), new RoundedCorners(20))
                .into(imageView);
    }


    /**
     * 加载一张普通的图片
     */
    @BindingAdapter("android:showImg")
    public static void showImg(ImageView imageView, String url) {

        Glide.with(imageView.getContext()).load(url)
                .placeholder(R.mipmap.ic_launcher_round)
                .error(R.mipmap.ic_launcher_round)
                .fallback(R.mipmap.ic_launcher_round)
                .into(imageView);
    }

    @BindingAdapter("android:escape")
    public static void escape(RTextView textView, String content) {
        if (content == null || content.isEmpty()) {
            return;
        }
        textView.setText(ExtsKt.removeEscapeCharacter(content));
    }



    /**
     * 加载固定宽高图片
     */
    @BindingAdapter({"android:imageUrl", "android:imageWidthDp", "android:imageHeightDp"})
    public static void imageUrl(ImageView imageView, String url, int imageWidthDp, int imageHeightDp) {
        Glide.with(imageView.getContext())
                .load(url)
                .override(DensityUtil.dip2px(imageView.getContext(), imageWidthDp), DensityUtil.dip2px(imageView.getContext(), imageHeightDp))
                .transition(DrawableTransitionOptions.withCrossFade(500))
                .placeholder(R.mipmap.ic_launcher_round)
                .centerCrop()
                .error(R.mipmap.ic_launcher_round)
                .into(imageView);
    }


}
